package com.kotlin.myapplicationd.base;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewConfiguration;


import com.kotlin.myapplicationd.MApplication;
import com.kotlin.myapplicationd.R;
import com.kotlin.myapplicationd.model.CommonPresenter;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;


import java.lang.reflect.Field;

public abstract class BaseActivity extends RxAppCompatActivity implements IBaseActivity {

    /**
     * 日志输出标志
     **/
    protected final String TAG = getClass().getSimpleName();
    /**
     * 共通操作
     **/
    protected BaseOperation mOperation = null;

    protected CommonPresenter mCommonPresenter;//共用的
    // -----------------------------------
    /***
     * 整个应用Applicaiton
     **/
    private MApplication mApplication = null;
    /**
     * 当前Activity渲染的视图View
     **/
    private View mContextView = null;
    protected Context mContext;
    //public UIHelper uiHelper;

    /**
     * 动画类型
     **/
    private int mAnimationType = NONE;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        Log.d(TAG, "BaseActivity-->onCreate()");
        //getSupportActionBar().hide();//隐藏标题栏

        // 获取应用Application
        mApplication = (MApplication) getApplicationContext();

        // 将当前Activity压入栈
        AppManager.getAppManager().addActivity(this);



        // 实例化共通操作
        mOperation = new BaseOperation(this);
        mCommonPresenter = new CommonPresenter(this);

        //mLoadingDialog = new LoadingDialog();
        // 初始化参数
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            mAnimationType = bundle.getInt(ANIMATION_TYPE, NONE);
        } else {
            bundle = savedInstanceState;
        }
        //uiHelper = UIHelper.attachToActivity(this);

        // 设置渲染视图View
        //View mView = bindView();
        //if (null == mView) {
        mContextView = LayoutInflater.from(this).inflate(bindLayout(), null);
        //} else {
        //	mContextView = mView;
        //}
        setContentView(mContextView);

        //StatusBarCompat.setStatusBarColor(this, Constants.DEFAULT_COLOR, 0);
        //获取上层传递数据
        initParams(bundle);

        // 初始化控件
        initView(mContextView);

        // 业务操作
        doBusiness(this);

        //监听事件
        initListener();



    }

    //protected void initRightButton(View view) {}

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "BaseActivity-->onRestart()");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "BaseActivity-->onResume()");
        //MobclickAgent.onResume(this);
        resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "BaseActivity-->onPause()");
        //MobclickAgent.onPause(this);
    }

    @Override
    protected void onStop() {


        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "BaseActivity-->onDestroy()");
        //uiHelper.destroy();
        destroy();

        AppManager.getAppManager().finishActivity(this);
    }


    /**
     * 显示Actionbar菜单图标
     */
    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        //		if (featureId == Window.FEATURE_ACTION_BAR && menu != null) {
        //			if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
        //				try {
        //					Method m = menu.getClass().getDeclaredMethod(
        //							"setOptionalIconsVisible", Boolean.TYPE);
        //					m.setAccessible(true);
        //					m.invoke(menu, true);// 显示
        //				} catch (Exception e) {
        //					Log.e(TAG, "onMenuOpened-->" + e.getMessage());
        //				}
        //			}
        //		}
        return super.onMenuOpened(featureId, menu);
    }

    /**
     * 显示OverFlowMenu按钮
     *
     * @param mContext 上下文Context
     */
    public void displayOverflowMenu(Context mContext) {
        try {
            ViewConfiguration config = ViewConfiguration.get(mContext);
            Field menuKeyField = ViewConfiguration.class
                    .getDeclaredField("sHasPermanentMenuKey");
            if (menuKeyField != null) {
                menuKeyField.setAccessible(true);
                menuKeyField.setBoolean(config, false);// 显示
            }
        } catch (Exception e) {
            Log.e("ActionBar", e.getMessage());
        }
    }

    /**
     * 获取当前Activity
     *
     * @return
     */
    //protected Activity getContext() {
    //	if (null != AppManager.getAppManager().currentActivity())
    //		return AppManager.getAppManager().currentActivity();
    //	else
    //		return null;
    //}

    /**
     * 获取共通操作机能
     */
    public BaseOperation getOperation() {
        return this.mOperation;
    }

    public CommonPresenter getGlobalCommonPresenter() {
        return this.mCommonPresenter;
    }


    /**
     * Actionbar点击返回键关闭事件
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    public void finish() {
        super.finish();
        switch (mAnimationType) {
            case IBaseActivity.LEFT_RIGHT:
                overridePendingTransition(0, R.anim.base_slide_right_out);
                break;
            case IBaseActivity.TOP_BOTTOM:
                overridePendingTransition(0, R.anim.push_up_out);
                break;
            case IBaseActivity.FADE_IN_OUT:
                overridePendingTransition(0, R.anim.fade_out);
                break;
            default:
                break;
        }
        mAnimationType = NONE;
    }


    public MApplication getmApplication() {
        return mApplication;
    }

}
